<?php defined('SYSPATH') or die('No direct script access.');

class Controller_BoDishCategory extends Controller_Layout {
	public $auth_required = TRUE;
	private $modelName = "dishCategory";
	
	public function action_index()
	{
	}

	public function action_insertPage(){
		$supplier = ORM::factory("supplier");
		$result_supplier = $supplier->where("status","<>","I")->find_all();
		$view = View::factory("pages/bo/dish_category_add_page");
		$view->result_supplier=$result_supplier;
		$view->supId = $_GET['supid'];
		exit($view->render());
	}

	public function action_insert(){
		$dish_category = ORM::factory("dishCategory");
		$_POST["status"] = "A";
		if($dish_category->values($_POST)->check()){
			if($dish_category->save()){
				ORM::factory("supplier",$_POST["supplierid"])->updateModifyTime();
				$view = View::factory("pages/bo/common/message_dialog");
				$view->message = "添加商家菜系成功";
				exit($view->render());
			}
		}
	}
	
	public function action_update(){
		$dish_category = ORM::factory("dishCategory",$_POST["id"]);
		$dish_category->name = $_POST["name"];
		if($dish_category->save()){
			ORM::factory("supplier",$dish_category->supplierid)->updateModifyTime();
			$view = View::factory("pages/bo/common/message_dialog");
			$view->message = "修改商家菜系成功";
			exit($view->render());
		}
	}

	public function action_show(){
		$dish_category = ORM::factory("dishCategory");
		$supplier_id = $_GET["supid"];
		$result_dish_category = $dish_category->where("supplierid","=",$supplier_id)->and_where("status","<>","I")->find_all();
		$view = View::factory("pages/bo/dish_category_page");
		$view->result_dish_category=$result_dish_category;
		$view->result_supplier_name = ORM::factory("supplier",$supplier_id)->displaySupplierName();
		exit($view->render());
	}

	public function action_deleteAll(){
		$status = "I";
		$dish_category = ORM::factory("dishCategory");
		$dish_category->status = $status;
		$ids = $_POST["ids"];
		foreach (split(",", $_POST["ids"]) as $id){
			if (!empty($id)){
				$dish_category->or_where("id","=",$id);
				if (empty($supplierId)){
					$supplierId = ORM::factory("dishCategory",$id)->supplierid;
				}
			}
		}
		$result_dish_category = $dish_category->save_all();
		if($this->updateStatus($result_dish_category, $status)){
			ORM::factory("supplier",$supplierId)->updateModifyTime();
			$view = View::factory("pages/bo/common/message_dialog");
			$view->message = "删除菜系成功";
			exit($view->render());
		}
	}
	
	public function action_dishCategoryData(){
		$result_dish_category = ORM::factory("dishCategory")->where("supplierid","=",$_REQUEST["supId"])->and_where("status","<>","I")->find_all();
		$html = "";
		foreach ($result_dish_category as $item){
			$html = $html.'<option value="'.$item->id.'"';
			$html = $html.'>'.$item->name.'</option>';
		}
		$this->request->response = $html;
	}
	
	public function action_copy(){
		$dish_category = ORM::factory("dishCategory");
		$ids = $_REQUEST["ids"];
		foreach (split(",", $_POST["ids"]) as $id){
			if (!empty($id)){
				$dish_category->or_where("id","=",$id);
			}
		}
		$copyStatus = true;
		$result_dish_category = $dish_category->find_all()->as_array();
		foreach ($result_dish_category as $dish_category) {
			$dish_category_id = $dish_category->id;
			$new_dish_category = ORM::factory("dishCategory");
			$new_dish_category = $this->cloneEntity($dish_category,$new_dish_category);
			$new_dish_category->supplierid = $_REQUEST["targetsupid"];
			$new_dish_category->copyfrom = $dish_category_id;
			if ($new_dish_category->save()){
				//3.copy dish
				$result_dish = ORM::factory("dish")->where("dish_cate_id","=",$dish_category_id)->and_where("status","<>","I")->find_all()->as_array();
				foreach ($result_dish as $dish){
					$dish_id = $dish->id;
					$new_dish = ORM::factory("dish");
					$new_dish = $this->cloneEntity($dish,$new_dish);
					$new_dish->dish_cate_id = $new_dish_category->id;
					$new_dish->supplierid = $_REQUEST["targetsupid"];
					$new_dish->copyfrom = $dish->id;
					if($new_dish->save()){
						$result_dish_item = ORM::factory("dishItem")->where("dishid","=",$dish_id)->and_where("status","<>","I")->find_all()->as_array();
						foreach ($result_dish_item as $dish_item){
							$new_dish_item = ORM::factory("dishItem");
							$new_dish_item = $this->cloneEntity($dish_item,$new_dish_item);
							$new_dish_item->dishid = $new_dish->id;
							$new_dish_item->dishcateid = $new_dish_category->id;
							$new_dish_item->supplierid = $_REQUEST["targetsupid"];
							$new_dish_item->copyfrom = $dish_item->id;
							if($new_dish_item->save()){
								
							}else{
								$copyStatus = false;
							}
						}
					}else{
						$copyStatus = false;
					}
				}
			}else{
				$copyStatus = false;
			}
		}
	} 
	
	private function cloneEntity($srcEntity,$newEntity){
		foreach ($srcEntity->list_columns() as $col) {
			$newEntity->__set($col["column_name"],$srcEntity->__get($col["column_name"]));
		}
		$newEntity->id = null;
		return $newEntity;
	}

	private function updateStatus($result_dish_category,$status){
		if ($result_dish_category){
			$dish = ORM::factory("dish");
			$dish->status = $status;
			foreach ($result_dish_category->find_all() as $subItem){
				$dish->or_where("dish_cate_id","=",$subItem->id);
			}
			return $dish->save_all();
		}
		return false;
	}
	
	public function action_removeCopyFlag(){
		$model = ORM::factory($this->modelName,$_REQUEST["id"]);
		$model->copyfrom = null;
		$model->save();
		
		$dishes = ORM::factory("dish")->where("dish_cate_id","=",$_REQUEST["id"]);
		$dishes->copyfrom = null;
		$dishes->save_all();
		
		$dish_items = ORM::factory("dishItem")->where("dishcateid","=",$_REQUEST["id"]);
		$dish_items->copyfrom = null;
		$dish_items->save_all();
	}
}
